package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.entity.SwitchVlan;
import com.ncloud.resman.service.impl.SwitchVlanServiceImpl;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by gaowei on 2017/9/1.
 */
@RestController
@RequestMapping("/resman/switchvlan")
public class SwitchVlanController {
	@Autowired
	private SwitchVlanServiceImpl switchVlanService;
    @GetMapping(value = "/vlanlist/{deviceId}")
    public Result<?> getSwitchVlanInfo(HttpServletRequest request,@PathVariable("deviceId") long deviceId){
    	JSONObject params = new JSONObject();
    	params.put("deviceId", deviceId);
    	JSONObject infos = switchVlanService.getSwitchVlanInfo(params);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/add")
    public Result<?> add(HttpServletRequest request, @RequestBody SwitchVlan switchVlan){
    	Object userID = request.getAttribute("userID");
    	switchVlan.setCreateUser(userID==null?1:Long.parseLong(userID.toString()));
    	switchVlan.setCreateDate(new Date());
    	int num = switchVlanService.insertSwitchVlan(switchVlan);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody SwitchVlan switchVlan){
    	Object userID = request.getAttribute("userID");
    	switchVlan.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	switchVlan.setUpdateDate(new Date());
    	int num = switchVlanService.updateSwitchVlan(switchVlan);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{vlanId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("vlanId") Long vlanId){
    	int num = switchVlanService.deleteSwitchVlan(vlanId);
		return ResultUtil.success(num);
    }
}
